﻿<%@ WebHandler Language="C#" Class="UserLoginLogsMgr" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;
using System.Data;
using System.Text;
using System.Collections.Generic;

public class UserLoginLogsMgr : AjaxHandlerBase
{
    public override void RequestProcess(HttpContext context)
    {
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        if (context.Request["op"] != null)
        {
            switch (context.Request["op"].ToLower())
            {
                case "getlist":
                    GetList(context);
                    break;
            }
        }
    }

    //获取列表信息
    public void GetList(HttpContext context)
    {
        string strWhere = "";
        string order = "Login_Time desc";

        int pageSize = 10;
        int pageIndex = 1;
        int count = 0;

        //判断查询权限
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserLogin_All))
        {
            strWhere = " 1=1 ";
        }
        //城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserLogin_City))
        {
            strWhere = " Login_CityId='" + CurrentUserInfo.CityId.ToString() + "' ";
        }
        //公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserLogin_Company))
        {
            strWhere = " Login_CompanyId='" + CurrentUserInfo.CompanyId.ToString() + "' ";
        }
        //没有权限
        else
        {
            strWhere = " 1=0 ";
        }
        
        
        if (context.Request["pagesize"] != null)
        {
            pageSize = Convert.ToInt32(context.Request["pagesize"]);
        }
        if (context.Request["pageindex"] != null)
        {
            pageIndex = Convert.ToInt32(context.Request["pageindex"]) + 1;
        }

        if (context.Request["UserCode"] != null)
        {
            strWhere += string.Format(" and Login_UserCode like '%{0}%'", context.Request["UserCode"]);
        }

        if (context.Request["Mac"] != null)
        {
            strWhere += string.Format(" and Login_Mac='{0}'", context.Request["Mac"]);
        }

        if (context.Request["Status"] != null)
        {
            strWhere += string.Format(" and Login_Status='{0}'", context.Request["Status"]);
        }

        if (context.Request["StartDate"] != null)
        {
            strWhere += string.Format(" and Login_Time>='{0}'", context.Request["StartDate"]);
        }
        if (context.Request["EndDate"] != null)
        {
            strWhere += string.Format(" and datediff(d,Login_Time,'{0}')>=0", context.Request["EndDate"]);
        }


        DataSet ds = Chenxin.Lib.Hr.DAL.Hr_UserLoginLogs.GetList(pageIndex, pageSize, order, strWhere, "*", "*", true);
        count = Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0]);

        context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(ds.Tables[0].Select("1=1")), count));

    }

    /// <summary>
    /// 生成列表
    /// </summary>
    /// <param name="drList"></param>
    /// <returns></returns>
    public string CreateGrid(DataRow[] drList)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<table style=\"width:100%\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        sb.Append("<thead><tr>");
        sb.Append("<th width=\"80px\" >登录人工号</th>");
        sb.Append("<th width=\"65px\" >登录人姓名</th>");
        sb.Append("<th width=\"80px\" >登录人部门</th>");
        sb.Append("<th width=\"55px\" >登录状态</th>");
        sb.Append("<th width=\"100px\" >错误描述</th>");
        sb.Append("<th>MAC地址</th>");
        sb.Append("<th width=\"120px\" >登录时间</th>");
        sb.Append("</tr></thead>");
        sb.Append("<tbody>");

        foreach (DataRow dr in drList)
        {
            sb.AppendFormat("<tr id=\"{0}\" class=\"tr_community\">", dr["Login_Id"]);
            sb.AppendFormat("<td>{0}</td>", dr["Login_UserCode"]);
            sb.AppendFormat("<td>{0}</td>", dr["Login_UserName"]);
            sb.AppendFormat("<td>{0}</td>", dr["Login_DepartName"]);
            sb.AppendFormat("<td>{0}</td>", dr["Login_Status"]);
            sb.AppendFormat("<td>{0}</td>", dr["Login_Description"]);
            sb.AppendFormat("<td>{0}</td>", dr["Login_Mac"] + "(" + dr["Login_Ip"] + ")");
            sb.AppendFormat("<td>{0}</td>", dr["Login_Time"]);
            sb.Append("</tr>");
        }
        sb.Append("</tbody>");
        sb.Append("</table>");

        return sb.ToString();
    }

}